package threading;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo25 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建任务
        //泛型参数写啥，call 方法的返回值类型就是啥
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for(int i=1;i<=1000;i++){
                    sum+=i;
                }
                return sum;
            }
        };
        //还需要找个人（线程），来完成这个任务
        //Thread 不能直接传 callable,需要包装一层
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();

        System.out.println(futureTask.get());
    }
}
